package org.jeecg.modules.jxc.statistical.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.jxc.statistical.entity.JxcPurchaseList;
import org.jeecg.modules.jxc.statistical.mapper.JxcPurchaseListMapper;
import org.jeecg.modules.jxc.statistical.service.IJxcPurchaseListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * @Description: jxc_purchase_list
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
@Service
@SuppressWarnings("all")
public class JxcPurchaseListServiceImpl extends ServiceImpl<JxcPurchaseListMapper, JxcPurchaseList> implements IJxcPurchaseListService {

    @Autowired
    private JxcPurchaseListMapper jxcPurchaseListMapper;

    @Override
    public List<Map<String, Object>> findSupplierJHAndTHTotalList(String begin, String end, String state, String key) {
        if (StringUtils.isEmpty(begin)){
            begin = null;
        }
        if (StringUtils.isEmpty(end)){
            end = null;
        }
        if (StringUtils.isEmpty(state)){
            state = null;
        }
        if (StringUtils.isEmpty(key)){
            key = null;
        }

        List<Map<String, Object>> list = jxcPurchaseListMapper.selectSupplierJHAndTHTotalList(begin,end,state,key);

        return list;
    }

    @Override
    public List<Map<String, Object>> findGoodsListDetail(String selectedNumber) {
        if (selectedNumber.startsWith("JH")){
            String purchaseId = jxcPurchaseListMapper.selectJHOneByNumber(selectedNumber);
            List<Map<String, Object>> JHMap = jxcPurchaseListMapper.selectJHGoodsListDetail(purchaseId);
            return JHMap;
        }
        if (selectedNumber.startsWith("TH")){
            String purchaseId = jxcPurchaseListMapper.selectTHOneByNumber(selectedNumber);
            List<Map<String, Object>> THMap = jxcPurchaseListMapper.selectTHGoodsListDetail(purchaseId);
            return THMap;
        }
        return null;
    }

    @Override
    public Boolean putJHPayState(Integer purchaseListId) {
        jxcPurchaseListMapper.updateJHPayStateById(purchaseListId);
        return true;
    }

    @Override
    public Boolean putXSPayState(Integer saleListId) {
        jxcPurchaseListMapper.updateXSPayStateById(saleListId);
        return true;
    }

    @Override
    public List<Map<String, Object>> findJHTotal(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
//        System.out.println(sTime);
//        System.out.println(eTime);
//        System.out.println(goodsTypeId);
//        System.out.println(codeOrName);
        if (StringUtils.isEmpty(sTime))
            sTime=null;

        if (StringUtils.isEmpty(eTime))
            eTime=null;

        // 查询code 或者 name
        if (StringUtils.isEmpty(codeOrName))
            codeOrName=null;
//        System.out.println("===================");
//        System.out.println(sTime);
//        System.out.println(eTime);
//        System.out.println(goodsTypeId);
//        System.out.println(codeOrName);
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectJHTotal(sTime,eTime,goodsTypeId,codeOrName);

        return list;
    }

    @Override
    public List<Map<String, Object>> findTHTotal(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        if (StringUtils.isEmpty(sTime))
            sTime=null;
        if (StringUtils.isEmpty(eTime))
            eTime=null;
        // 查询code 或者 name
        if (StringUtils.isEmpty(codeOrName))
            codeOrName=null;
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectTHTotal(sTime,eTime,goodsTypeId,codeOrName);
        return list;
    }

    @Override
    public List<Map<String, Object>> findXSTotal(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        if (StringUtils.isEmpty(sTime))
            sTime=null;
        if (StringUtils.isEmpty(eTime))
            eTime=null;
        // 查询code 或者 name
        if (StringUtils.isEmpty(codeOrName))
            codeOrName=null;
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectXSTotal(sTime,eTime,goodsTypeId,codeOrName);
        return list;
    }

    @Override
    public List<Map<String, Object>> findXTTotal(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        // 没有的就给其设置 null  sql中判断是null 的直接过滤不生成动态sql
        if (StringUtils.isEmpty(sTime))
            sTime=null;
        if (StringUtils.isEmpty(eTime))
            eTime=null;
        if (StringUtils.isEmpty(codeOrName))
            codeOrName=null;
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectXTTotal(sTime,eTime,goodsTypeId,codeOrName);
        return list;
    }

    @Override
    public List<Map<String, Object>> findDayProfitList(String sTime, String eTime) {
        if (StringUtils.isEmpty(sTime))
            sTime=null;
        if (StringUtils.isEmpty(eTime))
            eTime=null;
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectDayProfitList(sTime,eTime);
        return list;
    }

    @Override
    public List<Map<String, Object>> findMonthProfitList(String sTime, String eTime) {
        // sql 根据时间范围查询有哪些月份  前端只传 xxxx-xx 要进行自己更改

        if (StringUtils.isEmpty(sTime))
            sTime=null;
        else // 进行拼接  xxxx-xx  ===>  xxxx-xx-01
            sTime += "-01";

        if (StringUtils.isEmpty(eTime))
            eTime=null;
        else{
            // 进行拼接  xxxx-xx  ===>  xxxx-[x(x+1)]-01
            String monthStr = eTime.substring(5, 7);
            int month= Integer.parseInt(monthStr);
            if (month==2){
                eTime = eTime + "-28";
            }else {
                eTime = eTime + "-30";
            }
        }
        List<Map<String, Object>> list = jxcPurchaseListMapper.selectMonthProfitList(sTime,eTime);
        return list;
    }
}
